######################################################
# REPLICATION FILE FOR
# "Sustaining Capitalism and Democracy: Lessons from 
#     Global Competition Policy"
# By Michael O Allen and Kenneth Scheve
#
# IN THIS FILE:
#   Construct gravity data set
######################################################

library(data.table)

setwd('/Users/moda/Dropbox/Work/Yale/AntiTrust/Writing/ISR/Replication')

# Functions
# This creates a undirected dyad identifier used for merging some data
dyadID <- function (x) {
  dyad_id <- paste(min(x), max(x), sep = '_')
  return(dyad_id)
}

# Where do you want .dta saved?
path_write <- 'Data/full_data_gravity_robust.dta'

# Set file paths ----------------------------------------------------------
# The following data sets are required to generate the data set:

# Correlates of War Bilateral Trade and IO Data Sets
# https://correlatesofwar.org/data-sets
path_COWtrade <- '/Users/moda/Dropbox/Data/COW Bilateral trade/Dyadic_COW_4.0.csv'
path_COWio <- '/Users/moda/Dropbox/Data/IOs_treaties/COW_IOs/state_year_formatv3.csv'

# Comparative Competition Law Index
# https://comparativecompetitionlaw.org/data/
path_CLI <- '/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/CCL_Original/CCL_CLI_Data_Ver2.dta'

# DESTA
# https://www.designoftradeagreements.org/downloads/
path_DESTA <- '/Users/moda/Dropbox/Data/IOs_treaties/DESTA/desta_list_of_treaties_02_00_dyads_data.csv'
path_DESTA_content <- '/Users/moda/Dropbox/Data/IOs_treaties/DESTA/desta_version_02_00_data.csv'

# Currency Union
# https://sites.google.com/site/josedesousauniv/home/data-and-programs
path_comcur <- '/Users/moda/Downloads/comcur_2020.dta'

# World Bank WDI data is downloaded using WDI package


# Generate data set -------------------------------------------------------
# fullData will be the data set used for analysis

fullData <- data.table()

# Base COW Trade Data Set -------------------------------------------------

trade <- fread(path_COWtrade)
trade <- trade[year >= 1900]

# COW includes only one-observation per dyad-year
# This re-formats dataset to include separate observations for importer-year
#   and exporter-year for each country
for(countryCode in unique(trade$ccode1)) {
  fullData <- rbind(fullData, 
                    trade[ccode1 == countryCode, .(importer = importer1, importer_ccode = ccode1,
                                         exporter = importer2, exporter_ccode = ccode2, 
                                         year, imports = flow1)],
                    trade[ccode1 == countryCode, .(importer = importer2, importer_ccode = ccode2,
                                         exporter = importer1, exporter_ccode = ccode1, 
                                         year, imports = flow2)]
  )
}
rm(countryCode)

# Convert imports to dollars from millions of dollars
fullData[, imports := imports * 1000000]

# Set missing observations to missing
fullData[imports == -9000000 , imports := NA]

rm(trade, path_COWtrade)

# Merge with CLI ----------------------------------------------------------
CLI <- setDT(haven::read_dta(path_CLI))
CLI <- CLI[regional_group == 0, ]

CLI[ , importer_ccode := as.integer(code_cown)]
CLI[ , exporter_ccode := as.integer(code_cown)]

fullData <- merge(fullData, CLI[ , .(importer_ccode, year,
                                     cli_importer = cli_overall_norm_eu)],
                  by = c('importer_ccode', 'year'),
                  all.x = TRUE)

fullData <- merge(fullData, CLI[ , .(exporter_ccode, year,
                                     cli_exporter = cli_overall_norm_eu)],
                  by = c('exporter_ccode', 'year'),
                  all.x = TRUE)

rm(CLI, path_CLI)

# PTA (DESTA) -------------------------------------------------------------

# Dyadic PTA membership data
desta <- fread(path_DESTA)

# This contains data on content of PTAs
desta_content <- fread(path_DESTA_content)

desta <- merge(desta, desta_content[ , .(number, comp_chap)],
               by = 'number', 
               all.x = TRUE)

# Some agreements (like accession agreements) not merged, 
#   these are coded based on the base treaty's coding
#   Else assigned 0
#   E.g. China's accession to base treaty 100 is coded as 0 
#     because base treaty has no competition chapter
desta[, comp2 := ifelse(sum(comp_chap, na.rm = T) > 0, 1, 0), by = base_treaty]

desta$ccode1 <- countrycode::countrycode(desta$iso1, 'iso3n', 'cown')
desta$ccode2 <- countrycode::countrycode(desta$iso2, 'iso3n', 'cown')

# Remove observations with missing merging variables
desta <- desta[!is.na(ccode1)]
desta <- desta[!is.na(ccode2)]

# Create undirected dyad_ids for merging 
fullData$dyad_id <- apply(fullData[ , c('importer_ccode', 'exporter_ccode')], 1, dyadID)
desta$dyad_id <- apply(desta[ , c('ccode1', 'ccode2')], 1, dyadID)

# Creata data frame with dyadic entry into force of their first PTA
desta_pta_firstyears <- desta[!is.na(entryforceyear), 
                    .(firstYear = min(entryforceyear)), by = 'dyad_id']

fullData <- merge(fullData, desta_pta_firstyears, 
                  by = 'dyad_id', 
                  all.x = TRUE)

# Create data set with entry year of all PTAs with competition chapters
desta_comp <- desta[!is.na(entryforceyear) & comp2 == 1, 
                    .(firstYear_comp = min(entryforceyear)), by = 'dyad_id']

fullData <- merge(fullData, desta_comp, 
                  by = 'dyad_id', 
                  all.x = TRUE)

setorder(fullData, dyad_id, year)

fullData[year < firstYear, pta := 0, by = dyad_id]
fullData[year >= firstYear, pta := 1, by = dyad_id]
fullData[is.na(firstYear), pta := 0, by = dyad_id]
fullData$firstYear <- NULL

fullData[year < firstYear_comp, pta_comp := 0, by = dyad_id]
fullData[year >= firstYear_comp, pta_comp := 1, by = dyad_id]
fullData[is.na(firstYear_comp), pta_comp := 0, by = dyad_id]
fullData$firstYear_comp <- NULL

rm(desta, desta_comp, desta_content, desta_pta_firstyears, 
   path_DESTA, path_DESTA_content)

# Currency Union ----------------------------------------------------------
comcur <- setDT(haven::read_dta(path_comcur))

comcur$importer_ccode <- countrycode::countrycode(comcur$iso_d, 'iso3c', 'cown')
comcur$exporter_ccode <- countrycode::countrycode(comcur$iso_o, 'iso3c', 'cown')

fullData <- merge(fullData, 
                  comcur[ , .(exporter_ccode, importer_ccode, year, comcur)],
                  by = c('importer_ccode', 'exporter_ccode', 'year'),
                  all.x = TRUE)

rm(comcur, path_comcur)

# WTO Membership ----------------------------------------------------------

ios <- fread(path_COWio)

ios <- ios[ , .(ccode,  state,year, GATT, WTO)]

ios$gattwto <- ifelse(ios$WTO == 1 | ios$GATT == 1, 1, 0)

fullData <- merge(fullData, ios[ , .(importer_ccode = ccode, year, 
                                     gattwto_importer = gattwto)],
                  by = c('importer_ccode', 'year'),
                  all.x = T)

fullData <- merge(fullData, ios[ , .(exporter_ccode = ccode, year, 
                                     gattwto_exporter = gattwto)],
                  by = c('exporter_ccode', 'year'),
                  all.x = T)

fullData$wto_both <- ifelse(fullData$gattwto_exporter + fullData$gattwto_importer == 2, 1, 0)

rm(ios, path_COWio)

# Expenditure -------------------------------------------------------------
# World Bank data used in the paper were downloaded in July 2021.

wdi <- setDT(WDI::WDI(indicator = c(expenditure = 'NE.DAB.TOTL.CD')))
wdi$ccode <- countrycode::countrycode(wdi$iso2c, 'iso2c', 'cown')
wdi <- wdi[!is.na(ccode)]

fullData <- merge(fullData, wdi[ , .(importer_ccode = ccode, year, 
                                     expend_importer = expenditure)],
                  by = c('importer_ccode', 'year'),
                  all.x = TRUE)

fullData <- merge(fullData, wdi[ , .(exporter_ccode = ccode, year, 
                                     expend_exporter = expenditure)],
                  by = c('exporter_ccode', 'year'),
                  all.x = TRUE)
rm(wdi)

# Write -------------------------------------------------------------------

setorder(fullData, importer_ccode, exporter_ccode, year)

haven::write_dta(fullData, 'Data/gravity.dta')

